package org.esprit.ocm.client.model.oca.datasource;

import java.util.ArrayList;
import java.util.List;

import org.esprit.ocm.client.model.oca.ImageRecord;
import org.esprit.ocm.model.oca.ImageOca;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class ImageLocalDS extends DataSource {

	private static ImageLocalDS	instance	= null;

	public static ImageLocalDS getInstance(List<ImageOca> lists) {
//		if (instance == null) {
			instance = new ImageLocalDS("imageLocalDS", lists);
//		}
		return instance;
	}

	public ImageLocalDS(String id, List<ImageOca> lists) {

//		setID(id);

		DataSourceTextField idField = new DataSourceTextField("id", "id");
		DataSourceTextField uid = new DataSourceTextField("uid", "uid");
		DataSourceTextField username = new DataSourceTextField("username", "username");
		DataSourceTextField name = new DataSourceTextField("name", "name");
		DataSourceTextField type = new DataSourceTextField("type", "type");
		DataSourceTextField publicState = new DataSourceTextField("publicState", "publicState");
		DataSourceTextField persistent = new DataSourceTextField("persistent", "persistent");
		DataSourceTextField regtime = new DataSourceTextField("regtime", "regtime");
		DataSourceTextField source = new DataSourceTextField("source", "source");
		DataSourceTextField state = new DataSourceTextField("state", "state");
		DataSourceTextField running_vms = new DataSourceTextField("running_vms", "running_vms");

		DataSourceTextField templateOca_dev_prefix = new DataSourceTextField("templateOca_dev_prefix", "templateOca_dev_prefix");
		DataSourceTextField templateOca_name = new DataSourceTextField("templateOca_name", "templateOca_name");
		DataSourceTextField templateOca_path = new DataSourceTextField("templateOca_path", "templateOca_path");
		DataSourceTextField templateOca_type = new DataSourceTextField("templateOca_type", "templateOca_type");

		setFields(idField, uid, username, name, type, publicState, persistent, regtime, source, state, running_vms, templateOca_dev_prefix,
				templateOca_name, templateOca_path, templateOca_type);

		// setFields(templateOca_dev_prefix, templateOca_name, templateOca_path,
		// templateOca_type);

		List<ListGridRecord> l = new ArrayList<ListGridRecord>();
		for (ImageOca from : lists) {

			ImageRecord to = new ImageRecord();

			to.setId(from.getId());
			to.setUid(from.getUid());
			to.setUsername(from.getUsername());
			to.setName(from.getName());
			to.setType(from.getType());
			to.setPublicState(from.getPublicState());
			to.setPersistent(from.getPersistent());
			to.setRegtime(from.getRegtime());
			to.setSource(from.getSource());
			to.setState(from.getState());
			to.setRunning_vms(from.getRunning_vms());

			to.setTemplateOca_dev_prefix(from.getTemplateOca_dev_prefix());
			to.setTemplateOca_name(from.getTemplateOca_name());
			to.setTemplateOca_path(from.getTemplateOca_path());
			to.setTemplateOca_type(from.getTemplateOca_type());

			l.add(to);
		}

		for (ListGridRecord lgr : l) {
			System.out.println("+" + lgr.getAttribute("publicState"));
		}

		setTestData(l.toArray(new ImageRecord[0]));
		setClientOnly(true);
	}

}
